package com.sixth.stock.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sixth.core.entity.Purchase;
import com.sixth.core.entity.PurchaseItem;
import com.sixth.core.util.JWTUtil;
import com.sixth.core.util.WebUtil;
import com.sixth.core.vo.R;
import com.sixth.stock.service.PurchaseService;
import com.sixth.stock.vo.PurchaseDto;
import com.sixth.stock.vo.PurchaseItemDto;
import com.sixth.stock.vo.PurchaseItemDtos;
import com.sixth.stock.vo.PurchaseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@Api(tags = "采购管理,审查，入库")
@RestController
@RequestMapping("/stock/purchase")
public class PurchaseController {
    @Resource
    private PurchaseService purchaseService;
    //分页查询所有的采购单数据【主页面】
    @ApiOperation(value = "分页查询所有的采购单数据")
    @PostMapping("/listPurchaseForPage")
    public R<IPage<Purchase>> list(@RequestBody PurchaseVo purchaseVo){
        return purchaseService.selectByPage(purchaseVo);
    }
    // 分页查询所有的待审核采购单数据【入库审核页面】
    @ApiOperation(value = "分页查询所有的待审核采购单数据")
    @PostMapping("/listByStatus")
    public R<IPage<Purchase>> listByStatus(@RequestBody PurchaseVo purchaseVo){
        return purchaseService.listByStatus(purchaseVo);
    }
    // 提交审核【主页面】
    @ApiOperation(value = "提交审核")
    @PostMapping("/doAudit/{purchaseId}")
    public R submit(@PathVariable String purchaseId){
        return purchaseService.submit(purchaseId);
    }
    // 作废【主页面】
    @ApiOperation(value = "作废")
    @PostMapping("/doInvalid/{purchaseId}")
    public R cancel(@PathVariable String purchaseId){
        return purchaseService.doInvalid(purchaseId);
    }
    // 入库【主页面】
    @ApiOperation(value = "入库")
    @PostMapping("/doInventory/{purchaseId}")
    public R receive(@PathVariable String purchaseId){
        return purchaseService.doInventory(purchaseId);
    }
    // 审核通过【根据采购单号】【入库审核页面】
    @ApiOperation(value = "审核通过")
    @PostMapping("/auditPass/{purchaseId}")
    public R auditPass(@PathVariable String purchaseId){
        return purchaseService.auditPass(purchaseId);
    }
    // 审核不通过【根据采购单号】【入库审核页面】
    @ApiOperation(value = "审核不通过")
    @PostMapping("/auditNoPass/{purchaseId}/{reason}")
    public R auditNoPass(@PathVariable String purchaseId, @PathVariable String reason){
        return purchaseService.auditNoPass(purchaseId,reason);
    }
    // 根据采购单据ID查询采购详情信息
    @ApiOperation(value = "根据采购单据ID查询采购详情信息")
    @GetMapping("/getPurchaseItemById/{purchaseId}")
    public R<List<PurchaseItem>> getPurchaseItemById(@PathVariable String purchaseId){
        return purchaseService.getPurchaseItemById(purchaseId);
    }
    // 生成单据号【新增采购页面】【采购单号】
    @ApiOperation(value = "生成单据号", notes = "生成单据号,雪花算法，生成唯一单据号")
    @GetMapping("/generatePurchaseId")
    public R<String> generatePurchaseId(){
        return purchaseService.generatePurchaseId();
    }
    // 添加采购单和详情【新增采购页面】
    //暂存【订单id页面】
    @ApiOperation(value = "添加采购单和详情", notes = "暂存,仅仅详情，不提交审核")
    @PostMapping("/erpAddPurchase")
    public R erpAddPurchase(@RequestBody List<PurchaseItem>  purchaseItems){
        System.out.println("++++++++");
        System.out.println(purchaseItems.toString());
        System.out.println("++++++++");
        return purchaseService.erpAddPurchase(purchaseItems);
    }
    @ApiOperation(value = "结合erpAddPurchase方法，共同完成提交暂存功能，添加purchase", notes = "erpAddPurchase方法updateStatusPurchaseId，以提交审核")
    @PostMapping("/updateStatusPurchaseId")
    public R updateStatusPurchaseId(@RequestBody PurchaseDto purchaseDto){
        return purchaseService.updateStatusByPurchaseId(purchaseDto);
    }
    // 添加采购单和详情并提交审核【新增采购页面】   // 添加采购单和详情并提交审核【订单id页面】
    @ApiOperation(value = "添加采购单和详情并提交审核", notes = "添加采购单和详情并提交审核")
    @PostMapping("/addPurchaseToAudit")
    public R addPurchaseToAudit(@RequestBody List<PurchaseItem> purchaseItems){
        return purchaseService.addPurchaseToAudit(purchaseItems);
    }
    //结合上一个方法，共同完成提交审核功能，修改purchase，根据purchaseId修改
    @ApiOperation(value = "结合addPurchaseToAudit方法，共同完成提交审核功能，修改purchase，根据purchaseId修改", notes = "addPurchaseToAudit和updatePurchaseId方法结合使用，以提交审核")
    @PostMapping("/updatePurchaseId")
    public R updatePurchaseId(@RequestBody PurchaseDto purchaseDto){
        return purchaseService.updateByPurchaseId(purchaseDto);
    }

    // 根据采购单ID查询采购单信息和详情
    @ApiOperation(value = "根据采购单ID查询采购单信息和详情", notes = "入库信息")
    @GetMapping("/queryPurchaseAndItemByPurchaseId/{purchaseId}")
    public R<List<PurchaseItem>> queryPurchaseAndItemByPurchaseId(@PathVariable String purchaseId){
        return purchaseService.queryPurchaseAndItemByPurchaseId(purchaseId);
    }
    // 根据采购单ID查询采购单信息和详情
    @ApiOperation(value = "根据采购单ID查询采购单信息和详情", notes = "入库信息")
    @GetMapping("/queryPurchaseAndItemByPurchaseId2/{purchaseId}")
    public R<Map<String,Object>> queryPurchaseAndItemByPurchaseId2(@PathVariable String purchaseId){
        return purchaseService.queryPurchaseAndItemByPurchaseId2(purchaseId);
    }
}
